<?php

namespace app\beautiful\model\partner;

use app\common\model\CommonModel;

use app\beautiful\model\member\MemberModel;
use think\facade\Db;


class PartnerCommissionModel extends CommonModel{

    protected  $name = 'beautiful_partner_commission';

    protected  $pk   = 'commission_id';



    public function member()

    {

        return $this->hasOne(MemberModel::class,'member_id','member_id');

    }



    public function getMonetarySumByPartnerId($memberIds){

        $where = [

            ['member_id','IN',$memberIds],

        ];

        $datas = $this->field('member_id,SUM(monetary) AS money')->where($where)->group('member_id')->select();

        $return = [];

        foreach($datas as $val){

            $return[$val['member_id']] = $val['money'];

        }

        return $return;

    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_partner_commission')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->field("a.*,b.nick_name as nick_name");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['add_time']=date('Y-m-d H:i:s',$v['add_time']);
            }
        }
        return compact('count','list');
    }
}

